import wxcharts from '../../API/wxcharts.js'//引入
Page({
  data: {
    show: false,
    list: '',
    dqheight: 0,
    weight: 0,
    BMI: 0,
    height: 0,
    heights:{},
    heightscharge: [0, 0, 0, 0, 0],
    timescharge: ['0000-00-00', '0000-00-00', '0000-00-00', '0000-00-00', '0000-00-00'],
    autoBMI: 'BMI:自动计算',
    //data中配置
    ec: {
      lazyLoad: true
    }
  },
  
  showPopup() {
    this.setData({ show: true });
  },
  postdata:function(e) {
    var app = getApp()
    var ip = app.globalData.ServerAddress
    let height = e.detail.value.height
    let weight = e.detail.value.weight
    console.log(height)
    console.log(weight)
    wx.request({
      url: "http://" + ip + "/user/posture_recording",
      method: 'PUT',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data:{
        "user_id": app.globalData.user_id,
        "weight": weight,
        "height": height
      },
      success:function (res){
        console.log("成功！")
        // 
        that.onLoad()
      }
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  onLoad() {

    var app = getApp()
    var ip = app.globalData.ServerAddress
    console.log(ip)
    let that = this
      wx.request({
        url: "http://" + ip + "/user/posture_recording",
        method: 'POST',
        dataType: "json",
        header: {
          'Content-type': 'application/json',
          "authorization": app.globalData.token
        },
        data: {
          user_id: app.globalData.user_id
        },
        success: function (res) {
          // console.log(res)
          let ll = res.data.PostureRecording
          if(ll.length>0) {
            that.setData({
              // list: res.data.PostureRecording.length,
              heights: res.data.PostureRecording,
              weight: res.data.PostureRecording[0].weight,
              height: res.data.PostureRecording[0].height ,
              BMI: (res.data.PostureRecording[0].weight / (res.data.PostureRecording[0].height * res.data.PostureRecording[0].height * 0.0001)).toFixed(1),
              dqheight: res.data.PostureRecording[0].height,
            })
            let arr = []
        for(let i = ll.length - 1; i >= 0; i--) {
          that.data.heightscharge.push(ll[i].height)
          console.log(that.data.heightscharge)
          that.data.timescharge.push(ll[i].time.slice(0, 10))
          console.log(that.data.timescharge)
        }
            new wxcharts({
              canvasId: 'lineCanvas',
              extra: {
                lineStyle: 'curve'
              },
              type: 'line',
              categories: [that.data.timescharge[that.data.timescharge.length - 5], that.data.timescharge[that.data.timescharge.length - 4], that.data.timescharge[that.data.timescharge.length - 3], that.data.timescharge[that.data.timescharge.length - 2], that.data.timescharge[that.data.timescharge.length - 1]],
              series: [{
                  name: '身高',
                  data: [that.data.heightscharge[that.data.heightscharge.length - 5], that.data.heightscharge[that.data.heightscharge.length - 4], that.data.heightscharge[that.data.heightscharge.length - 3], that.data.heightscharge[that.data.heightscharge.length - 2], that.data.heightscharge[that.data.heightscharge.length - 1]],
                  format: function (val) {
                      return val.toFixed(1);
                  }
              }
              // , {
              //     name: '成交量2',
              //     data: [0.30, 0.37, 0.65, 0.78, 0.69, 0.94],
              //     format: function (val) {
              //         return val.toFixed(2) + '万';
              //     }
              // }
            ],
              yAxis: {
                  // title: '身高变化 (厘米)',
                  format: function (val) {
                      return val.toFixed(2);
                  },
                  min: 0
              },
              width: 380,
              height: 300,
          });
          }
          
        }
      })
      new wxcharts({
        canvasId: 'lineCanvas',
        extra: {
          lineStyle: 'curve'
        },
        type: 'line',
        categories: ['0', '0', '0', '0', '0', '0'],
        series: [{
            name: '身高',
            data: [0, 0, 0, 0, 0, 0],
            format: function (val) {
                return val.toFixed(1);
            }
        }
       
      ],
        yAxis: {
            // title: '体重变化 (公斤)',
            format: function (val) {
                return val.toFixed(2);
            },
            min: 0
        },
        width: 380,
        height: 300,
    });
  //   new wxcharts({
  //     canvasId: 'lineCanvas',
  //     extra: {
  //       lineStyle: 'curve'
  //     },
  //     type: 'line',
  //     categories: ['3/19', '3/19', '3/19', '3/20', '3/20', '3/20'],
  //     series: [{
  //         name: '身高',
  //         data: [180, 181, 182, 183, 184, 185],
  //         format: function (val) {
  //             return val.toFixed(1);
  //         }
  //     }
  //     // , {
  //     //     name: '成交量2',
  //     //     data: [0.30, 0.37, 0.65, 0.78, 0.69, 0.94],
  //     //     format: function (val) {
  //     //         return val.toFixed(2) + '万';
  //     //     }
  //     // }
  //   ],
  //     yAxis: {
  //         // title: '身高变化 (厘米)',
  //         format: function (val) {
  //             return val.toFixed(2);
  //         },
  //         min: 0
  //     },
  //     width: 380,
  //     height: 300,
  // });
  }
})
